init()

function init() {
    let clientServer = document.querySelector('.menu2 >ul')
    let indexGate = document.querySelector('.leftmenu>ul')
    pAjax({
        url: 'https://www.lemall.com/api/news/footerNews.html',
        dataType: 'json'
    }).then(res => {
        let str = '';
        res.data.forEach(item => {
            str += `
            <li><a>${ item.name}</a></li>
    `
        })
        clientServer.innerHTML = str;

    })
    pAjax({
        url: 'https://www.lemall.com/api/indexCate.html',
        dataType: 'json'
    }).then(res => {
        let str = '';
        res.data.cateList.forEach(item => {
            str += `
                <li>
                <a>${item.name}</a>
                <b>></b>
                <div class="nextmenu">
                <dl>
                `
            item.childs.forEach(item => {
                str += `
                    <dt data-urlid="${item.id}">
                        <a>${item.name}</a>
                        <span>></span>
                    </dt>
                    `
                str += `
                        <dd>`
                item.childs.forEach(item => {

                    str += `
                            <a data-did="${item.id}" class='ritem'>${item.name}</a>
                  
                    `
                })
                str += `  </dd>
                    `
            })
            str += `
                        </dl>
                     </div>
                     </li>
                     `
        })
        indexGate.innerHTML = str;
        clickHandler()

        function clickHandler() {
            indexGate.addEventListener('click', e => {
                const urlid = e.target.dataset.urlid
                const did = e.target.dataset.did
                window.sessionStorage.setItem("urlid", urlid)
                window.sessionStorage.setItem("did", did)
                if (e.target.nodeName === "DT") {
                    window.location.href = './list.html'
                }
                if (e.target.className === "ritem") {
                    window.location.href = './goodslist.html'
                }
            })
        }
    })
    const hotwords = document.querySelector('.hotwords')
    pAjax({
        url: 'https://www.lemall.com/api/searchKeyword.html',
        dataType: 'json'
    }).then(res => {
        let str = ``;
        str += `
        <b>热门搜索:</b>
        <div>
        `
        res.data.forEach(item => {
            str += `
                <a>${item}</a>
                `
        })
        str += `</div>`;
        hotwords.innerHTML = str;
    })
    bindHtml()
    bindList()
}
//渲染主要列商品
function bindHtml() {
    pAjax({
        url: 'https://www.lemall.com/api/indexFloor.html',
        dataType: 'json'
    }).then(res => {
        let str = ``;
        let skList = res.data.floorList.find(item => item.name === '新品上市')
        const skbox = document.querySelector('.sklist')
        skList.classList.forEach(item => {
            item.dataList.forEach(item => {
                str += `
                <li class="skitem" data-did="${item.product.id}">
                <img src="${item.product.masterImg}">
                <div class="pinfo">
                <div class="pname">
                <a href="">${item.product.name1}</a>
                </div>
                <div class="pprice">
                <b>￥${item.product.mallPcPrice}</b>
                </div>
                </li>
                `
            })
            skbox.innerHTML = str
        })
        skbox.addEventListener('click', e => {
            const did = e.target.dataset.did
            window.sessionStorage.setItem("did", did)
            window.location.href = './details.html'
        })
    })
}

function bindList() {
    const floor = document.querySelector('.floor')
    pAjax({
        url: 'https://www.lemall.com/api/indexFloor.html',
        dataType: 'json'
    }).then(res => {
        let str = ``;
        for (let i = 0; i < res.data.floorList.length; i++) {
            if (res.data.floorList[i].name === '新品上市') continue
            str += `
            <div class="floor-banner w1210">
                <img src="${res.data.floorList[i].advImage}" dataset-url="${res.data.floorList[i].advLinkUrl}">
            </div>
            <div class="pcshop w1210">
            <div class="top">
                <div class="top-list">
                <span class="title">${res.data.floorList[i].name }</span>
              
                `

            res.data.floorList[i].patchList.forEach(item => {
                str += `
                        <a data-did="${item.id}" class="tolist">${item.title}</a>
                        `
            })
            str += `
                <div class="more">
                    <a href="">更多</a>
                </div>
                </div>
                </div>
                <div class="list">
                <div class="left">
                <img src="${res.data.floorList[i].masterImage}" alt="">
                <p></p>
                </div>
                <div class="right">
                <ul>
                `
            res.data.floorList[i].classList.forEach(item => {
                item.dataList.forEach(item => {
                    str += `<li data-did=${item.product.id} class="goods">
                        <img src="${item.product.masterImg}" alt="">
                        <p>${item.product.name1}</p>
                        <p class="price">￥${item.product.mallPcPrice}</p>
                    </li>
                    `
                })
            })
            str += `
                </ul>
                </div>
                </div>
            </div>
            `
            floor.innerHTML = str;

            //点击事件
        }
        bindFloorBtn();

        const floorBox = document.querySelector('.floor')
        floorBox.addEventListener('click', e => {
            if (e.target.className === "tolist" || e.target.className === "goods") {
                const did = e.target.dataset.did
                window.sessionStorage.setItem('did', did)
                window.location.href = "./details.html"
            }
        })
    })
}

// 搜索栏事件
let goodsId;
const keywordBtn = document.querySelector('.keyword')
const searchList = document.querySelector('.search-nextbox')
searchHandler()

function callBackData(res) {
    if (!res.g) {
        searchList.style.display = 'none'
        return
    }
    let str = ''
    res.g.forEach(item => {
        str += `
        <li>${ item.q }</li>
      `
    })
    searchList.innerHTML = str
    searchList.style.display = 'block'
}

function searchHandler() {
    keywordBtn.addEventListener('input', () => {
        const scriptType = document.createElement('script')
        const text = keywordBtn.value.trim()
        scriptType.src = `https://www.baidu.com/sugrec?pre=1&p=3&ie=utf-8&json=1&prod=pc&from=pc_web&sugsid=35106,31253,35237,35435,34584,35246,34872,35316,26350&wd=${text}&req=2&bs=%E7%99%BE%E5%BA%A6&csor=7&pwd=asdas&cb=callBackData&_=1639482112387`
        document.body.appendChild(scriptType);
        scriptType.remove()

    })
}